package com.twu.biblioteca;

import java.util.HashMap;
import java.util.Scanner;

public class Library {
    static HashMap map = new HashMap<String, String>();
    boolean isLogin = false;

    public static void main(String args[]) {
        Library library = new Library();
        map.put("111-1111", "1111");
        map.put("111-1112", "1112");
        System.out.println("welcome to Library\n");
        while (true) {
            Scanner scanner = new Scanner(System.in);
            System.out.println(library.showMenu());
            library.menuAction(scanner);
        }
    }

    public String showMenu() {
        if (isLogin) {
            return "1.view list of books\n" +
                    "2.view list of movies\n" +
                    "3.check membership\n";
        }
        return "1.view list of books\n" +
                "2.view list of movies\n" +
                "3.login\n";
    }

    public void menuAction(Scanner scanner) {
        switch (scanner.nextInt()) {
            case 1:
                System.out.println("1.book one\n" +
                        "2.book tow\n");
                System.out.println(reserveBook(scanner.nextInt()));
                break;
            case 2:
                System.out.println(getMovieList());
                break;
            case 3:
                loginAndCheckMembership(scanner);
                break;
            default:
                System.out.println(showError());
        }
    }

    private void loginAndCheckMembership(Scanner scanner) {
        if (isLogin) {
            System.out.println("Yong, me@gmail.com, 111-11111111");
            return;
        }
        System.out.println("username:");
        String username = scanner.next();
        System.out.println("password:");
        String password = scanner.next();
        if (validAccount(username, password, map)) {
            System.out.println("log successful");
            return;
        }
        System.out.println("log failed");
    }

    public String reserveBook(int reserveNumber) {
        if (!isLogin) {
             return "Sorry, you can't reserve, please login";
        }
        if (reserveNumber == 1) {
            return "Thank You! Enjoy the book.";
        } else if (reserveNumber == 2) {
            return "Sorry we don't have that book yet.";
        }
        return showError();
    }

    public String showError() {
        return "Select a valid option!!";
    }

    public String getMovieList() {
        return  "   Movie          Year  Director  Rating\n" +
                "1  movie one      1999  Yang      2\n" +
                "2  movie two      2000  Yang      4\n" +
                "3  movie three    2001  Yang      5\n" +
                "4  movie four     2002  Yang      1\n" +
                "5  movie five     2003  Yang      3\n" +
                "6  movie six      2004  Yang      5\n" +
                "7  movie seven    2005  Yang      4\n" +
                "8  movie eight    2006  Yang      2\n" +
                "9  movie nine     2007  Yang      3\n" +
                "10 movie ten      2008  Yang      6\n" +
                "11 movie eleven   2009  Yang      1\n" +
                "12 movie twelve   2010  Yang      9\n" +
                "13 movie thirteen 2011  Yang      8\n" +
                "14 movie fourteen 2012  Yang      6\n" +
                "15 movie fifteen  2013  Yang      N/A\n";
    }

    public boolean validAccount(String username, String password, HashMap<String, String> map) {
        String realPassword = map.get(username);
        if (!realPassword.equals(password)) {
            return false;
        }
        isLogin = true;
        return true;
    }
}